package com.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Course;
import com.example.mapper.CourseMapper;
import com.example.service.CourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseMapper courseMapper;
    @Override
    public void add(Course course) {
        //设置默认已选人数为0
        course.setAlreadyNum(0);
        courseMapper.add(course);
    }

    @Override
    public PageInfo selectPage(Integer pageNum, Integer pageSize, Course course) {
        List<Course> list = null;
        PageHelper.startPage(pageNum,pageSize);
        //老师只能看到自己的课程
        String name = course.getName(); //课程名称
        Integer teacherId = course.getTeacherId();//教师id
        if(ObjectUtil.isNotEmpty(name)){
            if (ObjectUtil.isNotEmpty(teacherId)){
                list = courseMapper.selectByNameAndTeacherId(name,teacherId);
            }else {
                list = courseMapper.selectByName(name);
            }
        }else {
           if (ObjectUtil.isNotEmpty(teacherId)){
               list = courseMapper.selectByTeacherId(teacherId);
           }else {
               list = courseMapper.selectAll();
           }
        }

        return PageInfo.of(list);
    }

    @Override
    public void update(Course course) {
        courseMapper.updateById(course);
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }
}
